//
//  STFirstControl.m
//  YiYueStore
//
//  Created by zhouzhouzhou on 2018/10/25.
//  Copyright © 2018年 YuYiLian. All rights reserved.
//

#import "STFirstControl.h"

@implementation STFirstControl

static STFirstControl* obj;
+(instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        obj = [[STFirstControl alloc] init];
    });
    return obj;
}

-(void)setFirstControl{
    [self getLocation];
}

//获取经纬度。存储到user单例
-(void)getLocation{
    locationManager = [[AMapLocationManager alloc] init];
    locationManager.distanceFilter = 200;
    locationManager.locationTimeout = 2;
    locationManager.reGeocodeTimeout= 2;
    [locationManager setLocatingWithReGeocode:YES];
    locationManager.delegate = self;
    locationManager.reGeocodeLanguage = AMapLocationReGeocodeLanguageChinse;
    [locationManager requestLocationWithReGeocode:1 completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
        NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
        YLUserInfo * userInfo = [YLUserInfo sharedInstance];
        //TODO:填写用户假数据
        if(!userInfo.stylistId.length){
            userInfo.stylistId = @"692";
        }
        if(!userInfo.userID.length){
            userInfo.userID = @"1";
        }
        userInfo.selfLatitude =location.coordinate.latitude;
        userInfo.selfLongitude =location.coordinate.longitude;
        self.selfRegeocode = regeocode;
        
        if (self.locationSuccess) {
            self.locationSuccess(regeocode);
        }
    }];
}
-(void)sendLocation:(AMapLocationReGeocode *)regeocode{
    if (!regeocode) {
        return;
    }
    BasicRequest * request = [[BasicRequest alloc] initWithParams:@{  @"cityId": regeocode.citycode,@"districtId": regeocode.adcode, @"latitude": [NSString stringWithFormat:@"%f",[YLUserInfo sharedInstance].selfLatitude], @"location": regeocode.formattedAddress,@"longitude": [NSString stringWithFormat:@"%f",[YLUserInfo sharedInstance].selfLongitude],@"userId": [YLUserInfo sharedInstance].userID}];
    request.URL = [NSString stringWithFormat:@"%@userLocation/save",BASEURL];
    request.requestMethod = HttpRequestMethodRawPOST;
    request.isShowHUD = NO;
    [request startRequestWithSuccess:^(NSURLSessionTask *task, id responseObject) {
        
    } Failure:nil];
}
@end
